﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using TST.SharePointObjects.WebControls;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Administration;
using Microsoft.SharePoint.Utilities;

namespace TST.SharePointObjects.WebControls
{
    public class SelectedObject : UserControl
    {
        protected HyperLink linkSelectedSiteObject;
        protected Label labelIndexScope;
        protected DropDownList selectIndexScope;
        protected Label labelindexBy;
        protected DropDownList selectIndexBy;
        protected Button buttonSaveOptions;
        private SiteObject _selected;
        protected CheckBox indexObject;
        protected CheckBox indexItems;
        internal delegate void BeforeSaveHandler(SiteObject siteObject);
        internal event BeforeSaveHandler BeforeSave;
        internal delegate void OnSelectItemHandler(SiteObject siteObject);
        internal event OnSelectItemHandler SelectItem;
        private SharePointObjectsDatabase _database;

        public SharePointObjectsDatabase Database
        {
            get { return _database; }
            set { _database = value; }
        }

        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
            SetupSelectedObject();
            buttonSaveOptions.Click += new EventHandler(buttonSaveOptions_Click);
        }

        private void SetupSelectedObject()
        {
            if (!Page.IsPostBack)
            {
                // Index Scope
                selectIndexScope.AutoPostBack = false;
                selectIndexScope.Items.Add(new ListItem("-- Select an index scope --", string.Empty));
                switch (Globals.GetSiteObjectTypeFromUrl(Page.Request.Url))
                {
                    case SiteObjectType.FeatureDefinition:
                        ListItem farmDefaultIndexScope = new ListItem(Globals.GetScopeDescription(ObjectReferenceScope.Farm), ObjectReferenceScope.Farm.ToString());
                        farmDefaultIndexScope.Selected = true;
                        selectIndexScope.Items.Add(farmDefaultIndexScope);

                        // Index by
                        selectIndexBy.AutoPostBack = false;
                        selectIndexBy.Items.Add(new ListItem("-- Select a property --", string.Empty));
                        ListItem farmDefaultProperty = new ListItem(Globals.IndexByPropertyDescription(IndexByProperty.Id), IndexByProperty.Id.ToString());
                        farmDefaultProperty.Selected = true;
                        selectIndexBy.Items.Add(farmDefaultProperty);
                        
                        indexItems.Visible = false;
                        break;
                    default:
                        ListItem defaultIndexScope = new ListItem(Globals.GetScopeDescription(ObjectReferenceScope.Web), ObjectReferenceScope.Web.ToString());
                        defaultIndexScope.Selected = true;
                        selectIndexScope.Items.Add(defaultIndexScope);
                        selectIndexScope.Items.Add(new ListItem(Globals.GetScopeDescription(ObjectReferenceScope.SiteCollection), ObjectReferenceScope.SiteCollection.ToString()));
                        selectIndexScope.Items.Add(new ListItem(Globals.GetScopeDescription(ObjectReferenceScope.WebApplication), ObjectReferenceScope.WebApplication.ToString()));
                        selectIndexScope.Items.Add(new ListItem(Globals.GetScopeDescription(ObjectReferenceScope.Farm), ObjectReferenceScope.Farm.ToString()));

                        // Index by
                        selectIndexBy.AutoPostBack = false;
                        selectIndexBy.Items.Add(new ListItem("-- Select a property --", string.Empty));
                        ListItem defaultProperty = new ListItem(Globals.IndexByPropertyDescription(IndexByProperty.Id), IndexByProperty.Id.ToString());
                        defaultProperty.Selected = true;
                        selectIndexBy.Items.Add(defaultProperty);
                        selectIndexBy.Items.Add(new ListItem(Globals.IndexByPropertyDescription(IndexByProperty.DisplayName), IndexByProperty.DisplayName.ToString()));
                        selectIndexBy.Items.Add(new ListItem(Globals.IndexByPropertyDescription(IndexByProperty.InternalName), IndexByProperty.InternalName.ToString()));

                        indexItems.Visible = true;
                        break;
                }

            }
        }

        private void buttonSaveOptions_Click(object sender, EventArgs e)
        {
            SaveSiteInfo();
            SiteObjectStore siteObjects = new SiteObjectStore(SPFarm.Local.Id, SPContext.Current.Site.WebApplication.Id, SPContext.Current.Site.ID, SPContext.Current.Web.ID, Database);
            _selected.Index = indexObject.Checked;
            _selected.IndexItems = indexItems.Checked;
            _selected.IndexByProperty = (IndexByProperty)Enum.Parse(typeof(IndexByProperty), selectIndexBy.SelectedValue);
            _selected.IndexScope = (ObjectReferenceScope)Enum.Parse(typeof(ObjectReferenceScope), selectIndexScope.SelectedValue);
            OnBeforeSave(_selected);
            siteObjects.UpdateSiteObject(_selected);
            SPUtility.Redirect(Page.Request.Url.ToString(), SPRedirectFlags.UseSource, System.Web.HttpContext.Current);
        }

        internal void GetReferenceInfo(SiteObject selected)
        {
            if (selected != null && (_selected == null || string.Compare(selected.Id, _selected.Id) != 0))
            {
                OnSelectItem(selected);
                linkSelectedSiteObject.Text = selected.Name;
                if (string.IsNullOrEmpty(selected.Url))
                {
                    linkSelectedSiteObject.Target = string.Empty;
                    linkSelectedSiteObject.NavigateUrl = this.Page.Request.Url.ToString();
                }
                else
                {
                    linkSelectedSiteObject.Target = "_blank";
                    linkSelectedSiteObject.NavigateUrl = selected.Url;
                }
                linkSelectedSiteObject.NavigateUrl = selected.Url;
                _selected = selected;
            }
        }

        private void OnSelectItem(SiteObject selected)
        {
            if (SelectItem != null)
            {
                SelectItem(selected);
            }
        }

        private void OnBeforeSave(SiteObject selected)
        {
            if (BeforeSave != null)
            {
                BeforeSave(selected);
            }
        }

        protected override void OnPreRender(EventArgs e)
        {
            if (_selected != null)
            {
                indexObject.Checked = _selected.Index;
                indexItems.Checked = _selected.IndexItems;
                selectIndexBy.SelectedValue = _selected.IndexByProperty.ToString();
                selectIndexScope.SelectedValue = _selected.IndexScope.ToString();
            }
            base.OnPreRender(e);
        }

        private void SaveSiteInfo()
        {
            using (SPWeb rootWeb = SPContext.Current.Site.RootWeb)
            {
                SiteInfoStore info = new SiteInfoStore(SPContext.Current.Site.ID,
                    SPContext.Current.Web.ID,
                    SPContext.Current.Site.WebApplication.Id,
                    SPContext.Current.Site.WebApplication.Farm.Id,
                    SPContext.Current.Web.Title,
                    SPContext.Current.Web.Url,
                    rootWeb.Title,
                    rootWeb.Url, 
                    _database);
                info.SaveSiteInfo();
            }
        }

    }
}
